home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / Count 1.0.1 / Original < prev   
Encoding:
Text File  |  1995-12-01  |  3.8 KB  |  186 lines  |  [TEXT/CWIE]

  1.     count -  By:  Jeff Beadles    jeff@quark.WV.TEK.COM
  2.  
  3.  
  4.     This program will count from the starting number to the stop
  5.     number, using the character 'fs' as the field seperator.
  6.  
  7.     Note, that fs may be in several forms:
  8.     -A    will use the letter 'A'
  9.     --    will use a '-' as fs, and
  10.     -\011    will use a tab (Octal 011) as the fs.  (sh does the expansion.)
  11.  
  12.     Bugs may be sent to me if desired.
  13.     Please keep your flames to yourself.  What do you expect for free?
  14. _______This_Is_The_END________
  15. f test `wc -l < README` -ne 13; then
  16. echo 'shar: README was damaged during transit (should have been 13 lines)'
  17. i
  18. i        ; : end of overwriting check
  19. cho 'x - Makefile'
  20. f test -f Makefile; then echo 'shar: not overwriting Makefile'; else
  21. ed 's/^X//' << '________This_Is_The_END________' > Makefile
  22. #
  23. # Makefile for count.  This is a little overkill, but what the heck.
  24. # (This is public domain too!)
  25. # Written by:  Jeff Beadles
  26. # jeff@quark.WV.TEK.COM        ...tektronix!quark.wv!jeff
  27. #
  28.  
  29. CC = cc
  30. CFLAGS =
  31.  
  32. #For the executable file
  33. BINDIR=/usr/bin
  34.  
  35. count: count.c Makefile
  36.     $(CC) $(CFLAGS) count.c -o count
  37.  
  38. install: count
  39.     -strip count
  40.     cp count ${BINDIR}/count
  41.     chmod 755 ${BINDIR}/count
  42.  
  43. clean:
  44.     rm -f *.o core a.out
  45.  
  46. clobber: clean
  47.     rm -f count
  48.  
  49.  
  50. count.1
  51. .\"
  52. .\" @(#)count        1.0    05/09/89
  53. .\"
  54. .TH COUNT 1 "09 MAY 1989"
  55. .UC 4
  56. .SH NAME
  57. count \- count numbers from a start to a stop point.
  58. .SH SYNOPSIS
  59. .B count [-c] start stop
  60. .SH DESCRIPTION
  61. .I Count
  62. will count thru an integer sequence of numbers from
  63. .I Start
  64. to
  65. .I Stop
  66. with a newline after each number.
  67.  
  68. Optionally,
  69. .I -c
  70. may be on the command line.  This may be in one of two forms.
  71. .I -$
  72. will put a 
  73. .I $
  74. between each number.
  75. .I -040
  76. will put a space (Octal
  77. .I 040
  78. ) between each number.
  79.  
  80. .SH AUTHOR
  81. Jeff Beadles    jeff@quark.WV.TEK.COM
  82.  
  83. /*    Count.c  Released into the public domain on 05/09/89
  84.  *    Written by:  Jeff Beadles  jeff@quark.WV.TEK.COM
  85.  *      or ...!tektronix!quark.WV!jeff
  86.  *
  87.  *    NOTE:  This program is not supported by Tektronix, Inc.
  88.  *
  89.  *    This program will count from the starting number to the stop
  90.  *    number, using the character 'fs' as the field seperator.
  91.  *    Note, that fs may be in several forms:
  92.  *    -A    will use the letter 'A'
  93.  *    --    will use a '-' as fs, and
  94.  *    -\011    will use a tab (Octal 011) as the fs.  (sh does the expansion.)
  95.  *
  96.  *    Bugs may be sent to me if desired.
  97.  *    Please keep your flames to yourself.  What do you expect for free?
  98.  *
  99.  */
  100.  
  101.  
  102. #include <stdio.h>
  103. #include <ctype.h>
  104.  
  105. /*
  106.  *    Default field seperator
  107.  */
  108.  
  109. #ifndef FS
  110. #define FS '\n'
  111. #endif
  112.  
  113. int
  114. main(argc,argv)
  115. int argc;
  116. char **argv;
  117.  
  118. {
  119.     void usage();
  120.     int oatc();
  121.     int    start = 0;    /* Start count                */
  122.     int    stop  = 0;    /* Stop  count                */
  123.     int    pos   = 1;    /* Position in command line for parsing    */
  124.     char    fs    = FS;    /* Field Separator            */
  125.  
  126.     if ( argc < 2)
  127.         usage(argv[0]);        /* Does not return */
  128.  
  129.     if ( argv[1][0] == '-' ) {
  130.         if ( (isdigit(argv[1][1])) && (strlen(argv[1]) == 4) )
  131.             fs=oatc(argv[1] + 1);
  132.             else
  133.             fs = argv[1][1];
  134.         pos++;        /* On to the next arg... */
  135.     }
  136.     start = atoi(argv[pos++]);    /* Start here, and... */
  137.  
  138.     if ( argc <= pos)
  139.         usage(argv[0]);        /* Does not return */
  140.  
  141.     stop  = atoi(argv[pos]);     /* Stop here. */
  142.     if ( start >= stop)        /* Are they brain damaged? */
  143.     {
  144.         fprintf(stderr,"Error:  START must be less than STOP\n");
  145.         exit(-2);
  146.     }
  147.  
  148. /*
  149.    Yes, this is it.  It even prints a '\n' when done, if the fs != '\n' (Wow)
  150.  */
  151.     while ( start <= stop )
  152.         printf("%d%c",start++,( (start != stop) ? fs : '\n' ) );
  153. }
  154.  
  155. /*
  156.    Can you figure out this function with no comments?  Sure, you can.
  157. */
  158. void usage(program)
  159. char *program;
  160.  
  161. {
  162.     fprintf(stderr,"Usage: %s [ -c] start stop\n",program);
  163.     exit(-1);
  164. }
  165.  
  166. /*
  167.  *    octal ascii to char
  168.  */
  169.  
  170. int oatc(str)
  171. char *str;
  172.     {
  173.     int retval=0;
  174.     int pos=0;
  175.     int tmp=0;
  176.     int loop;
  177.     static int table[] = { 1, 8, 64 };   /* Powers of 8, to avoid POW */
  178.  
  179.  
  180.     for(loop=strlen(str) - 1; loop >= 0; loop--)
  181.         retval += ( (str[loop] - '0') * table[pos++] );
  182.  
  183.     return((char)retval);
  184. }
  185.  
  186.